﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FSky;
using System.Drawing;
using System.Numerics;
using System.Windows.Forms; // 确保引用了System.Windows.Forms命名空间
namespace idea0
{
    public class Bomb
    {
        public int X, Y; // 炸弹的格子位置
        public DateTime PlaceTime; // 放置时间
        public bool Exploded; // 是否已经爆炸
        public int PlusEffectCount; // 增强效果剩余次数
        public Bitmap BoardBitmap;
        private const int cellSize = 60;
        public object BombOwner { get; set; }
        public System.Drawing.Image bombImage { get; set; }
        public Bomb(int x, int y)
        {
            X = x;
            Y = y;
            PlaceTime = DateTime.Now;
            Exploded = false;
            string bombPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "gif", "bomb.png");
            bombImage = System.Drawing.Image.FromFile(bombPath);
        }

        private const int Up = 1;
        private const int Down = 2;
        private const int Left = 4;
        private const int Right = 8;

        /* private int CountBits(int mask)
         {
             int count = 0;
             while (mask != 0)
             {
                 count += mask & 1;
                 mask >>= 1;
             }
             return count;
         }*/

        public void Explode(World world)
        {
            if (!Exploded)
            {
                int noPropWallsMask = 0;
                if (Y - 1 >= 0 && world.BlackWalls.Board[Y - 1, X] == 1 && !world.WhitePieces.IsWhiteCell(X, Y - 1))
                    noPropWallsMask |= Up;
                if (Y + 1 < 13 && world.BlackWalls.Board[Y + 1, X] == 1 && !world.WhitePieces.IsWhiteCell(X, Y + 1))
                    noPropWallsMask |= Down;
                if (X - 1 >= 0 && world.BlackWalls.Board[Y, X - 1] == 1 && !world.WhitePieces.IsWhiteCell(X - 1, Y))
                    noPropWallsMask |= Left;
                if (X + 1 < 25 && world.BlackWalls.Board[Y, X + 1] == 1 && !world.WhitePieces.IsWhiteCell(X + 1, Y))
                    noPropWallsMask |= Right;

                int noPropWallsCount = BitOperations.PopCount((uint)noPropWallsMask);

                // 中心格子
                HandleExplosion(world, X, Y);
                AddExplosionEffect(world, X, Y, "center.png");

                // 上方向
                if (Y - 1 >= 0)
                {
                    HandleExplosion(world, X, Y - 1);
                    AddExplosionEffect(world, X, Y - 1, "top.png");
                }
                // 下方向
                if (Y + 1 < 13)
                {
                    HandleExplosion(world, X, Y + 1);
                    AddExplosionEffect(world, X, Y + 1, "bottom.png");
                }
                // 左方向
                if (X - 1 >= 0)
                {
                    HandleExplosion(world, X - 1, Y);
                    AddExplosionEffect(world, X - 1, Y, "left.png");
                }
                // 右方向
                if (X + 1 < 25)
                {
                    HandleExplosion(world, X + 1, Y);
                    AddExplosionEffect(world, X + 1, Y, "right.png");
                }

                // 根据PlusEffectCount扩展爆炸范围
                if (PlusEffectCount > 0)
                {
                    // 扩展一个格子
                    // 上
                    if (Y - 1 >= 0 && world.BlackWalls.Board[Y - 1, X] != 1)
                    {
                        HandleExplosion(world, X, Y - 1);
                        AddExplosionEffect(world, X, Y - 1, "top.png");
                    }
                    // 下
                    if (Y + 1 < 13 && world.BlackWalls.Board[Y + 1, X] != 1)
                    {
                        HandleExplosion(world, X, Y + 1);
                        AddExplosionEffect(world, X, Y + 1, "bottom.png");
                    }
                    // 左
                    if (X - 1 >= 0 && world.BlackWalls.Board[Y, X - 1] != 1)
                    {
                        HandleExplosion(world, X - 1, Y);
                        AddExplosionEffect(world, X - 1, Y, "left.png");
                    }
                    // 右
                    if (X + 1 < 25 && world.BlackWalls.Board[Y, X + 1] != 1)
                    {
                        HandleExplosion(world, X + 1, Y);
                        AddExplosionEffect(world, X + 1, Y, "right.png");
                    }
                    PlusEffectCount--;
                }

                // 根据noPropWallsCount扩展爆炸范围
                switch (noPropWallsCount)
                {
                    case 0:
                    case 1:
                        // 不需要进一步扩展
                        break;
                    case 2:
                        // 选择另外两个方向各加2个格子
                        List<(int dx, int dy)> expansionDirections = new List<(int, int)>();
                        if ((noPropWallsMask & Up) == 0 && Y - 1 >= 0)
                            expansionDirections.Add((0, -1));
                        if ((noPropWallsMask & Down) == 0 && Y + 1 < 13)
                            expansionDirections.Add((0, 1));
                        if ((noPropWallsMask & Left) == 0 && X - 1 >= 0)
                            expansionDirections.Add((-1, 0));
                        if ((noPropWallsMask & Right) == 0 && X + 1 < 25)
                            expansionDirections.Add((1, 0));

                        if (expansionDirections.Count >= 2)
                        {
                            for (int i = 0; i < 2; i++)
                            {
                                (int dx, int dy) direction = expansionDirections[i];
                                for (int j = 1; j <= 2; j++)
                                {
                                    int stepX = X + direction.dx * j;
                                    int stepY = Y + direction.dy * j;
                                    if (stepX >= 0 && stepX < 25 && stepY >= 0 && stepY < 13)
                                    {
                                        HandleExplosion(world, stepX, stepY);
                                        string effectPath = "";
                                        if (direction.dx == -1)
                                            effectPath = Path.Combine(Application.StartupPath, "gif", "left.png");
                                        else if (direction.dx == 1)
                                            effectPath = Path.Combine(Application.StartupPath, "gif", "right.png");
                                        else if (direction.dy == -1)
                                            effectPath = Path.Combine(Application.StartupPath, "gif", "top.png");
                                        else if (direction.dy == 1)
                                            effectPath = Path.Combine(Application.StartupPath, "gif", "bottom.png");
                                        if (!string.IsNullOrEmpty(effectPath))
                                        {
                                            ExplosionEffect effect = new ExplosionEffect(stepX, stepY, effectPath);
                                            world.ExplosionEffects.Add(effect);
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        // 选择另一个方向加3个格子
                        List<(int dx, int dy)> remainingDirections = new List<(int, int)>();
                        if ((noPropWallsMask & Up) == 0 && Y - 1 >= 0)
                            remainingDirections.Add((0, -1));
                        if ((noPropWallsMask & Down) == 0 && Y + 1 < 13)
                            remainingDirections.Add((0, 1));
                        if ((noPropWallsMask & Left) == 0 && X - 1 >= 0)
                            remainingDirections.Add((-1, 0));
                        if ((noPropWallsMask & Right) == 0 && X + 1 < 25)
                            remainingDirections.Add((1, 0));

                        if (remainingDirections.Count >= 1)
                        {
                            (int dx, int dy) direction = remainingDirections[0];
                            for (int i = 1; i <= 3; i++)
                            {
                                int stepX = X + direction.dx * i;
                                int stepY = Y + direction.dy * i;
                                if (stepX >= 0 && stepX < 25 && stepY >= 0 && stepY < 13)
                                {
                                    HandleExplosion(world, stepX, stepY);
                                    string effectPath = "";
                                    if (direction.dx == -1)
                                        effectPath = Path.Combine(Application.StartupPath, "gif", "left.png");
                                    else if (direction.dx == 1)
                                        effectPath = Path.Combine(Application.StartupPath, "gif", "right.png");
                                    else if (direction.dy == -1)
                                        effectPath = Path.Combine(Application.StartupPath, "gif", "top.png");
                                    else if (direction.dy == 1)
                                        effectPath = Path.Combine(Application.StartupPath, "gif", "bottom.png");
                                    if (!string.IsNullOrEmpty(effectPath))
                                    {
                                        ExplosionEffect effect = new ExplosionEffect(stepX, stepY, effectPath);
                                        world.ExplosionEffects.Add(effect);
                                    }
                                }
                            }
                        }
                        break;
                }

                Exploded = true;
            }
        }

        private void HandleExplosion(World world, int x, int y)
        {
            if (y >= 0 && y < 13)
            {
                if (world.BlackWalls.Board[y, x] == 1 && world.WhitePieces.IsWhiteCell(x, y))
                {
                    world.BlackWalls.Board[y, x] = 0;
                }
                else if (world.Player != null && world.Player.GridPosition == (x, y))
                {
                    world.Player.Life--;
                }
                else
                {
                    foreach (Class2_AI ai in world.AIs.ToList())
                    {
                        if (ai.GridPosition == (x, y))
                        {
                            if (this.BombOwner == world.Player)
                            {
                                ai.Life--;
                            }
                        }
                    }
                }
            }
        }
        private void AddExplosionEffect(World world, int x, int y, string effectName)
        {
            string effectPath = Path.Combine(Application.StartupPath, "gif", effectName);
            ExplosionEffect effect = new ExplosionEffect(x, y, effectPath);
            world.ExplosionEffects.Add(effect);
        }

        public void Draw(Graphics g)
        {
            // 计算炸弹图片的绘制位置，使其居中在格子内
            int x = X * cellSize;
            int y = Y * cellSize;
            int offsetX = (cellSize - bombImage.Width) / 3;
            int offsetY = (cellSize - bombImage.Height) / 3;
            g.DrawImage(bombImage, x + offsetX, y + offsetY);
        }
    }
}